package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.pzhyd.sc.biz.inter.IpowerBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.Menuinfo;
import com.pzhyd.sc.entity.Rminfo;
/** 
* @Project: webvote
* @Title: PowerBizImpl.java
* @Package com.pzhyd.sc.biz.impl
* @Description: 授权逻辑层
* @author Gaoshiwei 812257531@qq.com
* @date Jul 9, 2012 2:51:53 PM
* @Copyright: 2012 攀枝花移动 All rights reserved.
* @version V1.0  
 */
public class PowerBizImpl implements IpowerBiz {

	private IBaseDao ibd;//数据dao层，负责与数据库进行交互

	public IBaseDao getIbd() {
		return ibd;
	}

	public void setIbd(IBaseDao ibd) {
		this.ibd = ibd;
	}

   //获得指定角色号和菜单父亲孩子节点号的菜单信息
	public List<Menuinfo> serachMenu(int rid, int pid) {
		List<Menuinfo> lst = null;
		String hql = "select rm.menuinfo from Rminfo rm where rm.roleinfo.rid = ? and rm.menuinfo.mparentId = ? order by rm.menuinfo.mid";
		List parms = new ArrayList();
		parms.add(rid);
		parms.add(pid);
		try {
			lst = ibd.searchHql(hql, parms);
			if(lst!=null){
			for (Menuinfo m : lst) {
				m.setChildMenuList(serachMenu(rid, m.getMid()));
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

   //根据指定的mparentId查询菜单信息，都是递归的
	public List<Menuinfo> serachMenu(int pid) {
		List<Menuinfo> lst = null;
		String hql = "from Menuinfo m where m.mparentId = ?";
		List parms = new ArrayList();
		parms.add(pid);
		try {
			lst = ibd.searchHql(hql, parms);
			for (Menuinfo m : lst) {
				m.setChildMenuList(serachMenu(m.getMid()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}


	public boolean update(int rid, String[] mid) {
		String hql = "delete Rminfo rm where rm.roleinfo.rid = ?";
		List params = new ArrayList();
		params.add(rid);
		int count = 0;
		boolean flag=false;
		try {
			count=ibd.operateHql(hql, params);
			for (int i = 0; i < mid.length; i++) {
				Rminfo rm = new Rminfo();
				rm.getRoleinfo().setRid(rid);
				rm.getMenuinfo().setMid(Integer.parseInt(mid[i]));
				ibd.insert(rm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}
	
	/**
	 * 查询父菜单,以及再查询它所拥有的子菜单(根据用户编号)
	 */
	public List<Menuinfo> queryUserMenubyparentId(int userId, int parentId)
			throws Exception {
		String hql = "select rm.menuinfo from Rminfo as rm where rm.menuinfo.mparentId=? and rm.roleinfo.rid in (select ur.roleinfo.rid from UsersInfo as ur where ur.usersId=?)";
		List param = new ArrayList();
		param.add(parentId);
		param.add(userId);
		List<Menuinfo> list =this.ibd.searchHql(hql, param);
		//查询子菜单
		for(Menuinfo m : list){
			//执行递归调用
			System.out.println("子菜单编号："+m.getMid());
			List<Menuinfo> templist = this.queryUserMenubyparentId(userId, m.getMid());
			m.setChildMenuList(templist);
		}
		for(Menuinfo mi:list){
		   for(Menuinfo mzi:mi.getChildMenuList()){
			   System.out.println(mzi.getMname());
		   }
		}
		return list;
	}

	public List<Menuinfo> seachMenuByrid(int rid) {
		// TODO Auto-generated method stub
		List<Menuinfo> lst = null;
		String hql = "select rm.menuinfo from Rminfo rm where rm.roleinfo.rid = ?";
		List parms = new ArrayList();
		parms.add(rid);
		try {
		    lst =ibd.searchHql(hql, parms);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

}